//
//  AppendMultipleFishpondHeaderView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/22.
//

#import "AppendMultipleFishpondHeaderView.h"
#import "YTBaseSelectStringView.h"
#import "YTBaseEditorStringView.h"


@interface AppendMultipleFishpondHeaderView ()<UITextFieldDelegate>
///池塘类型
@property (nonatomic ,strong)  YTBaseSelectStringView * typeName;

///减号
@property (nonatomic ,strong) UIButton * reduceBtn;
///加号
@property (nonatomic ,strong) UIButton * plusBtn;
@property (nonatomic ,strong) UITextField * countLab;

@end
@implementation AppendMultipleFishpondHeaderView

+ (instancetype)NewView{
    AppendMultipleFishpondHeaderView * view = [AppendMultipleFishpondHeaderView new];
    view.backgroundColor = UIColor.whiteColor;
    view.clipsToBounds = YES;
    [view setupView];
    return view;
}

- (void)setupView{
    UIView * oneview = [UIView new];
    oneview.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:oneview];
    [self addSubview:oneview];
    [oneview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.top.offset(kwt(10));
        make.height.offset(kwt(50));
    }];
    
    UILabel * toplab = [DTKTool labelWithText:@"*新增池塘数量" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
    toplab.attributedText = [DTKTool AttributedStringWithStr:@"*新增池塘数量" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [oneview addSubview:toplab];
    [toplab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UITextField * countLab = [UITextField new];
    countLab.font = kCFont(17);
    countLab.textColor = KcolorHex(@"#222222");
    [oneview addSubview:countLab];
    countLab.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
    countLab.textAlignment = NSTextAlignmentCenter;
    countLab.backgroundColor = UIColor.whiteColor;
    countLab.delegate = self;
    [DTKTool YJBK:kwt(5) andView:countLab andWidth:1 andColor:KcolorHex(@"#D9D9D9")];
    self.countLab = countLab;
    [countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-47));
        make.centerY.offset(0);
        make.width.offset(kwt(115));
        make.height.offset(kwt(32));
    }];
    
    UIButton * reduceBtn = [UIButton buttonWithType:0];
    self.reduceBtn = reduceBtn;
    [reduceBtn setImage:[UIImage imageNamed:@"template_reduce1"] forState:UIControlStateNormal];
    [reduceBtn setImage:[UIImage imageNamed:@"template_reduce2"] forState:UIControlStateDisabled];
    [oneview addSubview:reduceBtn];
    [reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-167));
        make.centerY.offset(0);
        make.width.height.offset(kwt(32));
    }];
    reduceBtn.addTarget(self,@selector(reduceBtnClick));
    
    UIButton * plusBtn = [UIButton buttonWithType:0];
    self.plusBtn = plusBtn;
    [plusBtn setImage:[UIImage imageNamed:@"template_plus1"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"template_plus2"] forState:UIControlStateDisabled];
    [oneview addSubview:plusBtn];
    [plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.centerY.offset(0);
        make.width.height.offset(kwt(32));
    }];
    plusBtn.addTarget(self,@selector(plusBtnClick));
    
    MJWeakSelf
    YTBaseSelectStringView * typeName = [YTBaseSelectStringView NewView];
    self.typeName = typeName;
    typeName.forceFlag = YES;
    typeName.name = @"池塘类型";
    typeName.block = ^(NSString * _Nonnull content) {
        if(weakSelf.openTKLXBlock){
            weakSelf.openTKLXBlock(weakSelf.model);
        }
    };
    [self addSubview:typeName];
    [typeName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.offset(0);
        make.top.equalTo(oneview.mas_bottom).offset(kwt(5));
        make.height.offset(kwt(60));
    }];

    UIView * buttomview = [UIView new];
    [self addSubview:buttomview];
    buttomview.backgroundColor = KcolorHex(@"#ECEDEE");
    [buttomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.offset(kwt(130));
    }];
    
    UILabel * temp = [DTKTool labelWithText:@"池塘名称和编组必填，预计产量(非必填)" andFont:kFont(15) andTextColor:KcolorHex(@"#6A6A6A")];
    [buttomview addSubview:temp];
    [temp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(14));
        make.top.offset(kwt(12));
    }];

    UIView * bgview = [UIView new];
    bgview.backgroundColor = UIColor.whiteColor;
    [DTKTool YJBK:kwt(5) andView:bgview andWidth:0.5 andColor:KcolorHex(@"#E5E3E3")];
    [buttomview addSubview:bgview];
    [bgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(35));
    }];
    
    
    UIView * oneline = [UIView new];
    oneline.backgroundColor = KcolorHex(@"#E5E3E3");
    [bgview addSubview:oneline];
    [oneline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(50));
        make.bottom.top.offset(0);
        make.width.offset(0.5);
    }];
    
    UILabel * onelab = [DTKTool labelWithText:@"数量" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    onelab.textAlignment = NSTextAlignmentCenter;
    [bgview addSubview:onelab];
    [onelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.equalTo(oneline.mas_left);
        make.top.offset(kwt(15));
    }];
    
    UIView * tweline = [UIView new];
    tweline.backgroundColor = KcolorHex(@"#E5E3E3");
    [bgview addSubview:tweline];
    [tweline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(134));
        make.bottom.top.offset(0);
        make.width.offset(0.5);
    }];
    
    UILabel * twelab = [DTKTool labelWithText:@"*池塘名称" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    twelab.attributedText = [DTKTool AttributedStringWithStr:@"*池塘名称" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kFont(15) andPattenr:@"[*]"];
    [bgview addSubview:twelab];
    [twelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(oneline.mas_right).offset(kwt(8));
        make.right.equalTo(tweline.mas_left);
        make.centerY.equalTo(onelab);
    }];
    
    UIView * threeline = [UIView new];
    threeline.backgroundColor = KcolorHex(@"#E5E3E3");
    [bgview addSubview:threeline];
    [threeline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(266));
        make.bottom.top.offset(0);
        make.width.offset(0.5);
    }];
    
    UILabel * threelab = [DTKTool labelWithText:@"*池塘编组" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    threelab.attributedText = [DTKTool AttributedStringWithStr:@"*池塘编组" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kFont(15) andPattenr:@"[*]"];
    [bgview addSubview:threelab];
    threelab.numberOfLines = 2;
    [threelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tweline.mas_right).offset(kwt(8));
        make.right.equalTo(threeline.mas_left);
        make.centerY.equalTo(onelab);
    }];
    
    UILabel * fourlab = [DTKTool labelWithText:@"*池塘名称" andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    fourlab.attributedText = [DTKTool AttributedStringWithStr:@"预计产量\n(斤)" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#787A79") andFont:kFont(15) andPattenr:@"[(斤)]"];
    [bgview addSubview:fourlab];
    fourlab.numberOfLines = 2;
    [fourlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(threeline.mas_right).offset(kwt(8));
        make.right.offset(0);
        make.centerY.equalTo(onelab);
    }];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if(textField.text.intValue < 2){
        textField.text = @"2";
        if(self.model.addPondModelList.count > 1){
            [self.model.addPondModelList removeObjectsInRange:NSMakeRange(1, self.model.addPondModelList.count - 2)];
        }
    }
    if(textField.text.intValue > 100){
        textField.text = @"100";
        if(self.model.addPondModelList.count > 100){
            [self.model.addPondModelList removeObjectsInRange:NSMakeRange(99, self.model.addPondModelList.count - 99)];
        }
    }
    if(self.model.addPondModelList.count > textField.text.intValue){
        [self.model.addPondModelList removeObjectsInRange:NSMakeRange(1, self.model.addPondModelList.count - textField.text.intValue)];
    } else if(self.model.addPondModelList.count < textField.text.intValue){
        int index = textField.text.intValue - self.model.addPondModelList.count;
//        NSLog(@"%d-%d-%d",index,textField.text.intValue,self.model.addPondModelList.count);
        for (int i = 0; i < index; i++) {
            [self.model.addPondModelList addObject:[addPondModelListModel new]];
        }
    }
    
    
    if(self.openReloadBlock){
        self.openReloadBlock();
    }
}

///减
- (void)reduceBtnClick{
    [self.model.addPondModelList removeLastObject];
    if(self.openReloadBlock){
        self.openReloadBlock();
    }
    
}
///加
- (void)plusBtnClick{
    if(self.model.addPondModelList.count > 99){
        self.plusBtn.enabled = NO;
        return;
    }
    [self.model.addPondModelList addObject:[addPondModelListModel new]];
    if(self.openReloadBlock){
        self.openReloadBlock();
    }
}
-(void)setModel:(AddPondModel *)model {
    _model = model;
    self.typeName.content = model.typeName;
    self.countLab.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.model.addPondModelList.count];
    [self.reduceBtn setEnabled: (self.model.addPondModelList.count > 2)];
    self.plusBtn.enabled = (self.model.addPondModelList.count < 100);
}
@end
